package com.xiaoxu.spzx.manager.config;

import com.xiaoxu.spzx.manager.interceptor.LoginAuthInterceptor;
import com.xiaoxu.spzx.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName:WebMVCConfiguration
 * Description: 开启跨域请求
 *
 * @author: 徐廷泉
 * @Create 2024/1/14 14:16
 */

@Component
public class WebMVCConfiguration implements WebMvcConfigurer {


    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor ;


    @Autowired
    private UserAuthProperties userAuthProperties;
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //拦截器的拦截方式
        registry.addInterceptor(loginAuthInterceptor)
                //读取配置文件 排除路径
                .excludePathPatterns(userAuthProperties.getNoAuthUrls())
                .addPathPatterns("/**");
    }

    //跨域 解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")      // 添加路径规则
                .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")           // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*") ;                // 允许所有的请求头
    }
}
